package com.cqgcxy.shopping.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.CommException;
import com.cqgcxy.shopping.common.JdbcUtils;
import com.cqgcxy.shopping.domain.Getaddress;



public class AddressDao  {
	private Logger logger=LoggerFactory.getLogger(getClass());
	private Getaddress rowMap(ResultSet rs) throws SQLException{
		Getaddress getaddress=new Getaddress();
		getaddress.setArea(rs.getString("area"));
		getaddress.setAddress(rs.getString("address"));
		getaddress.setConsigneename(rs.getString("consigneename"));
		getaddress.setConsigneephonenum(rs.getString("consigneephonenum"));
		getaddress.setPostcode(rs.getString("postcode"));
		return getaddress;
		}
		
		
		public  Getaddress findByCode(String usercode) {
			Connection conn = JdbcUtils.getConnection();
			PreparedStatement ps=null;
			ResultSet rs=null;
			try {
				String sql="select * from user where usercode=?";
				ps=conn.prepareStatement(sql);
				ps.setString(1, usercode);
				logger.debug(sql);
				rs=ps.executeQuery();
				
				while (rs.next()) {
					return rowMap(rs);
					}
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new CommException(e.getMessage());
			}finally {
				JdbcUtils.close(conn, ps, rs);
			}
			return null;
		}
		public void insert(Getaddress getaddress) {
			Connection conn = JdbcUtils.getConnection();
			PreparedStatement ps=null;
			ResultSet rs=null;
		try {
			String sql="insert into address(area,address,consignee_name,consignee_phonenum,postcode,usercode) values(?,?,?,?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1, getaddress.getArea());
			ps.setString(2, getaddress.getAddress());
			ps.setString(3, getaddress.getConsigneename());
			ps.setString(4, getaddress.getConsigneephonenum());
			ps.setString(5, getaddress.getPostcode());
			ps.setString(6, getaddress.getUsercode());
		
			if(ps.executeUpdate()< 1) {
				throw new CommException("添加地址异常");
			}
			logger.debug("添加地址成功");
		}catch (Exception e) {
			throw new CommException("添加地址异常:"+e.getMessage());
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		
	}
		public void update(Getaddress getaddress) {
			Connection conn=JdbcUtils.getConnection();
			PreparedStatement ps=null;
			ResultSet rs=null;
			try {
				logger.debug("修改数据");
				String sql="update address set area=?,address=?,consignee_name=?, "
						+ "consignee_phonenum=?,postcode=?,where usercode=?";
				ps=conn.prepareStatement(sql);
				ps.setString(1, getaddress.getArea());
				ps.setString(2, getaddress.getAddress());
				ps.setString(3, getaddress.getConsigneename());
				ps.setString(4, getaddress.getConsigneephonenum());
				ps.setString(5, getaddress.getPostcode());
				ps.setString(6, getaddress.getUsercode());
				logger.debug(sql);
				
				if(ps.executeUpdate()!=1) {
					throw new CommException("修改数据失败");
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new CommException(e.getMessage());
			} finally {
				JdbcUtils.close(conn, ps, rs);
			}

		}

}
